package org.dromara.business.service.impl;

import org.dromara.business.mapper.AppUserMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.RebateRecordBo;
import org.dromara.business.domain.vo.RebateRecordVo;
import org.dromara.business.domain.RebateRecord;
import org.dromara.business.mapper.RebateRecordMapper;
import org.dromara.business.service.IRebateRecordService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Collection;

/**
 * 返利记录Service业务层处理
 *
 * @author Eric
 * @date 2025-09-05
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RebateRecordServiceImpl implements IRebateRecordService {

    private final RebateRecordMapper baseMapper;

    /**
     * 查询返利记录
     *
     * @param id 主键
     * @return 返利记录
     */
    @Override
    public RebateRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询返利记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 返利记录分页列表
     */
    @Override
    public TableDataInfo<RebateRecordVo> queryPageList(RebateRecordBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<RebateRecord> lqw = buildQueryWrapper(bo);
//        Page<RebateRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<RebateRecordVo> page = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(page);
    }

    /**
     * 查询符合条件的返利记录列表
     *
     * @param bo 查询条件
     * @return 返利记录列表
     */
    @Override
    public List<RebateRecordVo> queryList(RebateRecordBo bo) {
        LambdaQueryWrapper<RebateRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RebateRecord> buildQueryWrapper(RebateRecordBo bo) {
        LambdaQueryWrapper<RebateRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(RebateRecord::getId);
        lqw.eq(bo.getUserId() != null, RebateRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getStakingId() != null, RebateRecord::getStakingId, bo.getStakingId());
        lqw.eq(bo.getRebateAmount() != null, RebateRecord::getRebateAmount, bo.getRebateAmount());
        lqw.eq(bo.getRebateDate() != null, RebateRecord::getRebateDate, bo.getRebateDate());
        lqw.eq(bo.getDayNumber() != null, RebateRecord::getDayNumber, bo.getDayNumber());
        lqw.eq(bo.getStatus() != null, RebateRecord::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增返利记录
     *
     * @param bo 返利记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(RebateRecordBo bo) {
        RebateRecord add = MapstructUtils.convert(bo, RebateRecord.class);
        validEntityBeforeSave(add);
        add.setCreateTime(LocalDateTime.now());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改返利记录
     *
     * @param bo 返利记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(RebateRecordBo bo) {
        RebateRecord update = MapstructUtils.convert(bo, RebateRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RebateRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除返利记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
